//
//  GXAddMenuCell.m
//  YIChuSeller
//
//  Created by 田小椿 on 2018/11/29.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXAddMenuCell.h"
#import "GXGroupSelectController.h"
#import "GXCommonIntroduceController.h"
#import "ImagePickerTool.h"
#import "OrderAndUploadPhotoMananger.h"
#import "MenuContentManager.h"
@interface GXAddMenuCell ()
@property (weak, nonatomic) IBOutlet UIImageView *photoImageView;
@property (weak, nonatomic) IBOutlet UITextField *menuNameTF;
@property (weak, nonatomic) IBOutlet UITextField *priceTF;
@property (weak, nonatomic) IBOutlet UITextField *dicountPriceTF;
@property (weak, nonatomic) IBOutlet UILabel *menuLabel;
@property (weak, nonatomic) IBOutlet UILabel *groupLabel;
@property (copy, nonatomic)NSString * imageStr;
@property (copy, nonatomic)NSString * groupId;
@property (copy, nonatomic)NSString * describleStr;
@property (strong, nonatomic) UIImage *selectImage;

@end
@implementation GXAddMenuCell
- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}
- (IBAction)pressSaveBtn:(UIButton *)sender {
    if (![self judgeValue]) {
        return;
    }
    self.describleStr = [self setViewValueNull:self.describleStr];
    self.imageStr = [self setViewValueNull:self.imageStr];
    NSString * storeId = [AccountTool sharedAccountTool].account.sotres[@"storeId"];
    [MenuContentManager addDishWithStoreID:storeId dishesName:self.menuNameTF.text dishesImage:self.imageStr describerStr:self.describleStr dishesId:self.groupId priceStr:self.priceTF.text discountPrice:self.dicountPriceTF.text success:^(id responseObject) {
        if (responseObject) {
         [[NSNotificationCenter defaultCenter] postNotificationName: kNotificationAddMenu object:nil];
        }
    } fail:^(id responseObject) {
    }];
    
}
- (IBAction)pressMenuDescribeBtn:(id)sender {
    GXCommonIntroduceController * introduceVC =[[GXCommonIntroduceController alloc]init];
    introduceVC.titleName = @"菜品描述";
    introduceVC.placeHolderStr = @"请输入菜品描述(选填)";
    WeakSelf(weakSelf);
    introduceVC.describleBlock = ^(NSString * _Nonnull describleStr) {
        weakSelf.menuLabel.text = @"已填写";
        self.describleStr = describleStr;
    };
    [self.getViewController.navigationController pushViewController:introduceVC animated:YES];
}
- (IBAction)pressGroupBtn:(id)sender {
    WeakSelf(weakSelf);
    GXGroupSelectController * selectVC = [[GXGroupSelectController alloc]init];
    selectVC.selectBlock = ^(GXMenuGroupModel * _Nonnull model) {
        weakSelf.model = model;
        weakSelf.groupLabel.text = model.name;
        weakSelf.groupId = model.dishesId;
    };
    [self.getViewController.navigationController pushViewController:selectVC animated:YES];
}
- (BOOL)judgeValue
{
    if ([NSString isBlankString:self.imageStr]) {
        [SVProgressHUD showErrorWithStatus:@"添加菜品照片失败"];
        return NO;
    }
    if ([NSString isBlankString:self.menuNameTF.text]) {
        [SVProgressHUD showErrorWithStatus:@"请输入菜品名"];
        return NO;
    }
    
    if ([NSString isBlankString:self.priceTF.text]) {
        [SVProgressHUD showErrorWithStatus:@"请输入原价格"];
        return NO;
    }
    if (![NSString inputShouldNumber:self.priceTF.text]) {
        [SVProgressHUD showErrorWithStatus:@"输入价格应为数字"];
        return NO;
    }
    if ([NSString isBlankString:self.groupId]) {
        [SVProgressHUD showErrorWithStatus:@"请选择分组"];
        return NO;
    }
    return YES;
}
- (NSString *)setViewValueNull:(NSString *)str
{
    if ([NSString isBlankString:str]) {
        return @"";

    }
    return str;
 
}
//添加照片
- (IBAction)pressAddMenuButton:(UIButton *)sender {

    [[ImagePickerTool sharePicker] showImagePicker:self.getViewController allowEdit:YES complete:^(id param) {
        [OrderAndUploadPhotoMananger uploadPhotoToServerFromImage:param fileName:MenuPic success:^(id responseObject) {
            if (responseObject) {
                self.photoImageView.image = param;
                self.imageStr = responseObject[@"describa"];
            }
        } fail:^(id responseObject) {
        }];
    }];
}


@end
